package v1

import (
	"bc/service"
	"bc/util/library/zstack"
	"bc/util/library/zstack/server"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
)

type image_server struct {

}
func (t *image_server) Create(c *gin.Context){

	oauth ,err := zstack.Oauth()
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}

	zone_uuid ,ok := c.GetPostForm("zone_uuid")
	if !ok {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请选择区域",
		})
		return
	}

	hostname,ok := c.GetPostForm("hostname")
	if !ok{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请输入镜像服务器IP",
		})
		return
	}
	uname,ok := c.GetPostForm("username")
	if !ok{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请输入用户名",
		})
		return
	}
	pwd,ok := c.GetPostForm("password")
	if !ok {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请输入密码",
		})
		return
	}
	sshport := c.DefaultPostForm("ssh_port","22")
	type_one := c.DefaultPostForm("type","ImageStoreBackupStorage")
	url,ok := c.GetPostForm("url")
	if !ok {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请输入URL",
		})
		return
	}
	name,ok := c.GetPostForm("name")
	if !ok {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请输入名称",
		})
		return
	}

	body := make(map[string]map[string]interface{})
	body["params"]=map[string]interface{}{
		"hostname":hostname,
		"username":uname,
		"password":pwd,
		"sshPort":sshport,
		"type":type_one,
		"url":url,
		"name":name,
		//"zoneUuid":"53349478546441128bcc54977482d594",
		//"importImages":false,
	}

	bodyStr,err := json.Marshal(body)

	if err != nil{
		c.JSON(500,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}

	result ,err := server.ImageBackupStorage.AddImageStoreBackupStorage(oauth,zone_uuid,string(bodyStr))
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":result,
	})

}
func (t *image_server)CreateCallBack(c *gin.Context){

	b,e := ioutil.ReadAll(c.Request.Body)
	if e != nil{
		panic(e)
	}
	defer c.Request.Body.Close()
	m := make(map[string]interface{})

	err := json.Unmarshal(b,&m)
	if err != nil{
		panic(err)
	}

	if v,ok := m["error"];ok{
		con := v.(map[string]interface{})["details"].(string)
		LogAdd(c,con, "镜像管理")
		return
	}
	nm := m["inventory"].(map[string]interface{})

	createm := make(map[string]interface{})
	createm["uuid"] = nm["uuid"]


	zone_uuid := c.Query("zone_uuid")
	oauth := c.Query("oauth")

	res,err := server.ImageBackupStorage.AttachBackupStorageToZone(zone_uuid,oauth,createm["uuid"].(string))

	if err != nil{
		panic(err)
	}

	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":res,
	})
	return

}
/**
	挂载镜像服务器至区域    回调方法
 */
func (t *image_server) ToZoneCallBack(c *gin.Context){

	b,e := ioutil.ReadAll(c.Request.Body)
	if e != nil{
		panic(e)
	}
	defer c.Request.Body.Close()

	m := make(map[string]interface{})
	err := json.Unmarshal(b,&m)
	if err != nil{
		panic(err)
	}

	if v,ok := m["error"];ok{
		con := v.(map[string]interface{})["details"].(string)
		LogAdd(c,con, "镜像管理")
		return
	}
	nm := m["inventory"].(map[string]interface{})
	ct := make(map[string]interface{})

	ct["uuid"] = nm["uuid"]
	ct["hostname"] = nm["hostname"]
	ct["username"] = nm["username"]
	ct["ssh_port"] = nm["sshPort"]
	ct["name"] = nm["name"]
	ct["url"] = nm["url"]
	ct["total_capacity"] = nm["totalCapacity"]
	ct["available_capacity"] = nm["availableCapacity"]
	ct["type"] = nm["type"]
	ct["state"] = nm["state"]
	ct["status"] = nm["status"]
	zones ,err :=json.Marshal(nm["attachedZoneUuids"])
	if err != nil{
		panic(err)
	}
	ct["zone_uuid"] = string(zones)
	ct["uid"] = c.GetInt("uid")
	bo,int := service.ImageServer.Create(ct)
	if bo == true {
		fmt.Println(int)
	}
}
func (t *image_server) Delete(c *gin.Context){

	uuid := c.Param("uuid")
	if uuid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"uuid必填",
		})
		return
	}
	mode := "Permissive"
	str,err := server.ImageBackupStorage.DeleteBackupStorage(uuid,mode)
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":str,
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":str,
	})
}
/**
	删除镜像服务器   回调
 */
func (t *image_server)DelImgServerCallBack(c *gin.Context){

	uuid := c.Query("uuid")

	if uuid == ""{
		panic("错误处理")
	}
	byte,err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		panic(err)
	}

	m := make(map[string]interface{})
	err = json.Unmarshal(byte,&m)
	if err != nil {
		panic(err)
	}

	b := service.ImageServer.Delete(map[string]interface{}{"uuid":uuid})
	if !b{
		panic("未知错误")
	}

}

var ImageServer = &image_server{}
